package com.yellowpage.tool.messageutil;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;




public class AESUtils {
	private static final Logger loger = Logger.getLogger(AESUtils.class);

    public static String encrypt4AES(String source,String key) { 
    	try { 
    			key = Md5Util.encode(key);
    			System.out.println(key);
	    		IvParameterSpec zeroIv = new IvParameterSpec(key.getBytes("utf-8")); 
	    		SecretKeySpec key1 = new SecretKeySpec(key.getBytes(), "AES"); 
	    		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
	    		cipher.init(Cipher.ENCRYPT_MODE, key1, zeroIv);
	    		byte[] encryptedData = cipher.doFinal(source.getBytes());
	    		String encryptResultStr = Base64.encode(encryptedData); 
	    		return encryptResultStr; 
    		/*IvParameterSpec zeroIv = new IvParameterSpec(key.getBytes());
            SecretKeySpec key1 = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key1, zeroIv);
            byte[] encryptedData = cipher.doFinal(source.getBytes());
            String encryptResultStr = Base64.encode(encryptedData);
            return encryptResultStr;*/
    		// 加密 
    		} catch (NoSuchAlgorithmException e) {   
//         		  e.printStackTrace();  

	      } catch (NoSuchPaddingException e) {   
//	              e.printStackTrace();   

	      } catch (InvalidKeyException e) {   
//	              e.printStackTrace();   

	     /* }  catch (IllegalBlockSizeException e) {   
//	              e.printStackTrace();   
	    	  LoggerUtil.error(LoggerUtil.StackTrace()+e.getMessage());
	      } catch (BadPaddingException e) {   
//	              e.printStackTrace();   
	    	  LoggerUtil.error(LoggerUtil.StackTrace()+e.getMessage());*/
	      }  catch (Exception e) { 

		} 
    	return null; 
    } 
    public static String decrypt4AES(String content,String key) { 
    	try { 
	    		key = Md5Util.encode(key);
	    		byte[] decryptFrom = Base64.decode(content); 
	    		IvParameterSpec zeroIv = new IvParameterSpec(key.getBytes("utf-8")); 
	    		SecretKeySpec key1 = new SecretKeySpec(key.getBytes(), "AES"); 
	    		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
	    		cipher.init(Cipher.DECRYPT_MODE, key1, zeroIv); 
	    		byte decryptedData[] = cipher.doFinal(decryptFrom); 
	    		return new String(decryptedData); // 加密 
    		} catch (NoSuchAlgorithmException e) {   
	           		  e.printStackTrace();  

		      } catch (NoSuchPaddingException e) {   
		              e.printStackTrace();   

		      } catch (InvalidKeyException e) {   
		              e.printStackTrace();   

		      } catch (UnsupportedEncodingException e) {   
		              e.printStackTrace();   

		      } catch (IllegalBlockSizeException e) {   
		              e.printStackTrace();   

		      } catch (BadPaddingException e) {  
		              e.printStackTrace();   

		      }  catch (Exception e) { 
		    	  e.printStackTrace();   

    		} 
    		return null; 
	} 
    /**
     * 数组转换成十六进制字符串
     * @param byte[]
     * @return HexString
     */
    public static final String bytesToHexString(byte[] bArray) {
     StringBuffer sb = new StringBuffer(bArray.length);
     String sTemp;
     for (int i = 0; i < bArray.length; i++) {
      sTemp = Integer.toHexString(0xFF & bArray[i]);
      if (sTemp.length() < 2)
       sb.append(0);
      sb.append(sTemp.toUpperCase());
     }
     return sb.toString();
    }


	/*
    public static void main(String args[]){
    	try {
    		RequestMSMark request = new RequestMSMark();
//    		request.setTimestamp("3");
    		request.setAppKey(Constant.SIGN_MoS);
    		request.setTypeMark("访问调研电话");
    		request.setSubjectNum("04007160721");
    		request.setImei("123");
    		request.setImsi("1234567890");
    		String info = JSON.toJSONString(request);
    		info = "{\"queryNum\":\"051986709308\",\"imsi\":\"460023104082738\",\"imei\":\"004401722062326\",\"timestamp\":\"1437813336287\",\"subjectNum\":\"051986709308\",\"channelno\":\"221\"}";
    		info = "{\"queryNum\":\"051986709308\",\"imsi\":\"460023104082738\",\"imei\":\"004401722062326\",\"timestamp\":\"1437813336287\",\"subjectNum\":\"051986709308\",\"channelno\":\"221\"}";
    		String str = encrypt4AES(info,"NEW&@LSDJ");
    		loger.fatal(str);
    		loger.fatal(decrypt4AES(str,"NEW&@HWDJ"));
    		
//    		str = "WaFioNJ8/g921Y02NGeGdBv9imZTEHoTuYcJ2sH75XiFWPhIk5BzeYtC3u2q4AFL3+fDtXko1rJX3fzTGQ0646EEo183bjTTpN2vRaY7HTU=";
//    		loger.fatal(decrypt4AES("","NEW&@HWDJ"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    */
}
